package com.miyu.client1.service.magic.impl;

import com.alibaba.fastjson.JSONArray;
import com.miyu.client1.dao.book.bookChapterDao;
import com.miyu.client1.dao.book.bookMagicDao;
import com.miyu.client1.pojo.book.BookChapter;
import com.miyu.client1.pojo.book.BookListInfo;
import com.miyu.client1.service.magic.BookMagicService;
import com.miyu.client1.utils.magic.GetBeanUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;

public class saveSearchTask implements Callable<JSONArray>{


    private JSONArray jo;
    private String type ;
    public saveSearchTask(JSONArray t , String type ) {
        this.jo = t;
        this.type = type;
    }

    @Override
    public JSONArray call() throws Exception {

        //service.saveBookListInfo(jo);
        if(type.equals("search")){
            bookMagicDao dao = (bookMagicDao) GetBeanUtil.getBeanByClass(bookMagicDao.class);
            dao.saveBookListInfo(jo);
        }

        else  if(type.equals("detail")){
            bookChapterDao dao = GetBeanUtil.getBeanByClass(bookChapterDao.class);
            dao.saveBookChapter(jo);
        } else if(type.equals("BookListInfo")){
            bookMagicDao dao = GetBeanUtil.getBeanByClass(bookMagicDao.class);
           List<BookListInfo> bli = jo.toJavaList(BookListInfo.class);
            dao.saveBatch(bli,800);

        }else if(type.equals("BookChapter")){
            bookChapterDao dao = GetBeanUtil.getBeanByClass(bookChapterDao.class);
            List<BookChapter> cli = jo.toJavaList(BookChapter.class);
            List<String> sqlList = new ArrayList<>();
            StringBuffer sql = new StringBuffer();
            for (BookChapter bc:cli) {
                sql.append("insert into book_chapter  (chapter_id,book_id,chapter_source,chapter_num,chapter_name,chapter_context)");
                sql.append(" values (");
                sql.append(" '").append(bc.getChapterId()).append("',");
                sql.append(" '").append(bc.getBookId()).append("',");
                sql.append(" '").append(bc.getChapterSource()).append("',");
                sql.append(" '").append(bc.getChapterNum()).append("',");
                sql.append(" '").append(bc.getChapterName()).append("',");
                sql.append(" '").append(bc.getChapterContext()).append("')");
                sqlList.add(sql.toString());
                sql.setLength(0);
            }
            dao.doExecuteBatch(sqlList);
          //  dao.saveBatch(cli,800);
        }

        return null;
    }
}
